package comm_service

import (
	"AlgorithmPractice/src/common/algo_utils"
	"AlgorithmPractice/src/common/comm_entity/db_entity"
	"AlgorithmPractice/src/common/midware_conn/DBConn/db_opera"
	"context"
	"fmt"
)

/**
 * @author 蔚蔚樱
 * @version V1.0
 * @date 2023/5/10 01:14
 * @author-Email micromicrohard@outlook.com
 * @blogURL https://blog.csdn.net/Micro_Micro_Hard
 * @description
 * */
// serve for db_entity.AlgoUTData
type AlgoUTDataService struct {
	*db_opera.AlgoUTDateOpera
}

func NewAlgoUTDataService() *AlgoUTDataService {
	return &AlgoUTDataService{
		db_opera.NewAlgoUTDateOpera(nil),
	}
}

// Change the data in the old database to the format of the new database，then sync to new database local
func (a *AlgoUTDataService) FormatOldUTDataToNewDBAndSyncLocalDB(ctx context.Context) {

	// read from old ut data source
	result, err := algo_utils.NewDBConnTools().UTDateQueryAll()
	if err != nil {
		fmt.Print(err)
		return
	}

	// trans to new ut data
	newUTDataArr := make([]*db_entity.AlgoUTData, 0)
	for _, entity := range result {
		entityTemp := entity
		sd := new(db_entity.AlgoUTData).TransUTDataEntity(entityTemp)
		newUTDataArr = append(newUTDataArr, sd)
	}

	// batch insert into new ut DB
	err = a.BatchInsertUTDate(ctx, newUTDataArr)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
}
